﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PasswordProvider.ObjectModel;
using PasswordProvider.ViewModel.Framework;
using PasswordProvider.ViewModel.Properties;
using System.Globalization;

namespace PasswordProvider.ViewModel
{
    public class GenericSiteDetailsViewModel : SiteDetailsViewModel
    {
        #region Construction
       
        private LogOnSite _nullSite = new LogOnSite(null, null, null, null);

        internal GenericSiteDetailsViewModel(GenericSite site, bool isNew)
            : base(site, isNew)
        {
        }
        
        #endregion

        #region Public Properties

        public ListSelectViewModel<LogOnSite> Website
        {
            get
            {
                if (_webSite == null)
                {
                    GenericSite generic = (GenericSite)Site;
                    LogOnSite webSite = generic.Website;

                    List<LogOnSite> listOfSites = GetLogOnSites();
                    listOfSites.Sort((a, b) => String.Compare(a.Name, b.Name, true, CultureInfo.CurrentUICulture));
                    listOfSites.Insert(0, _nullSite);

                    _webSite = new ListSelectViewModel<LogOnSite>(
                        Strings.Generic_WebSite_DisplayName, 
                        Strings.Generic_WebSite_ToolTip,
                        listOfSites,
                        webSite);
                    _webSite.SelectedItemChanged += delegate { OnPropertyChanged("Website"); };
                }
                return _webSite;
            }
        } private ListSelectViewModel<LogOnSite> _webSite;


        /// <summary>
        /// The credit card number.
        /// </summary>
        public EncryptedValueViewModel Data
        {
            get
            {
                if (_data == null)
                {
                    _data = new EncryptedValueViewModel(
                        Strings.Generic_Data_DisplayName,
                        Strings.Generic_Data_ToolTip, 16,
                        (Site as GenericSite).Data);
                    _data.PasswordChanged += delegate { OnPropertyChanged("Data"); };
                }
                return _data;
            }
        } private EncryptedValueViewModel _data;

        /// <summary>
        /// ShowHiddenValues: Whether or not to show the credit card number, security code, and pin.
        /// </summary>
        public ToggleViewModel ShowHidden
        {
            get
            {
                if (_showHidden == null)
                {
                    _showHidden = new ToggleViewModel(
                        Strings.Generic_ShowHidden_DisplayName,
                        Strings.Generic_ShowHidden_ToolTip,
                        Data.ShowClearText);
                    _showHidden.IsCheckedChanged += new ValueChangedEventHandler<bool>(ShowHiddenValues_IsCheckedChanged);

                }
                return _showHidden;
            }
        } private ToggleViewModel _showHidden;


        #endregion

        #region Overrides

        protected override void TransferProperties()
        {
            Data.CommitNewValue();

            GenericSite generic = (GenericSite)Site;
            generic.Website = Website.SelectedItem == _nullSite ? null : Website.SelectedItem;
        }

        #endregion

        #region Private


        private static List<LogOnSite> GetLogOnSites()
        {
            List<LogOnSite> list = new List<LogOnSite>();

            // Add the existing LogOnSites
            foreach (Site site in PasswordProviderData.Current.Sites)
            {
                LogOnSite logOn = site as LogOnSite;
                if (logOn != null)
                    list.Add(logOn);
            }

            return list;

        }

        /// <summary>
        /// Show or hide the card number, pin and security code based on ShowHiddenValues.IsChecked.
        /// </summary>
        void ShowHiddenValues_IsCheckedChanged(bool isChecked)
        {
            Data.ShowClearText = isChecked;
        }

        #endregion
    }
}
